const api = require('../../api/api.js');
const app = getApp();

Page({
  data: {
    imgUrl: app.globalData.imgUrl + 'siteImage/',
    sites: [],
    enterprises: []
  },
  onLoad() {
    let that = this;
    let imgUrl = that.data.imgUrl;
    let sites;
    wx.getStorage({
      key: 'userId',
      success: function(res) {
        api.queryPlantation({
          data: {
            account: res.data
          },
          success(res) {
            console.log(res.data);
            let sites = res.data.plantation;
            let enterprises = res.data.enterprise;
            sites.map(i => {
              if (i.image === 'no') {
                i.image = imgUrl + 'noImage.png';
              } else {
                i.image = imgUrl + i.image;
              }
            });
            that.setData({
              sites,
              enterprises
            });
          }
        });
        // api.querySitesByUserId({
        //   data: {
        //     account: res.data
        //   },
        //   success(res) {
        //     console.log(res.data);
        //     sites = res.data.sites;
        //     sites.map((i) => {
        //       api.querySiteImages({
        //         data: {
        //           siteId: i.siteId
        //         },
        //         success(res) {
        //           let imgs = res.data.siteImages;
        //           if (imgs.length === 0) {
        //             i.image = that.data.imgUrl + 'noImage.png';
        //           } else {
        //             i.image = that.data.imgUrl + imgs[0].image;
        //           }
        //           that.setData({
        //             sites: sites
        //           });
        //         }
        //       });
        //     });
        //     console.log(sites);
        //   }
        // });
      },
    })
  },
  goToSiteInfoPage(e) {
    let siteId = e.currentTarget.dataset.siteid;
    let entId = e.currentTarget.dataset.entid;
    wx.navigateTo({
      url: `/pages/siteinfo-a/siteinfo-a?siteId=${siteId}&entId=${entId}`,
    })
  },
  goToAddSitePage() {
    wx.navigateTo({
      url: `/pages/addsite/addsite`,
    })
  }
})